install.packages('lme4')
install.packages('dplyr')
install.packages('sjPlot')
library(lme4)
library(dplyr)
library(sjPlot)

#### Figure 1 ####

load('figure1_data.rdata')

figure1 <- ggplot(data = figure1_data, aes(x = Cong, y = pct_suspension)) + 
  geom_line(aes(linetype = Type)) + scale_x_continuous(breaks = seq(93,113,by = 1)) + scale_y_continuous(breaks = seq(0,100, by = 10), limits = c(0, 100), sec.axis = dup_axis(name = 'Percentage of Laws')) + labs(x = 'Congress', y = 'Percentage of House-Passed Measures') + geom_point(aes(x = Cong, y = pct_suspension)) + theme_bw()

figure1 <- figure1 + theme(legend.position = 'bottom')

figure1

#### Figure 2 ####


load('susp_bills_by_type.rdata')

figure2 <- ggplot(susp_bills_by_type, aes(fill=commemorative, y=n, x=Cong)) + 
  geom_bar(position="dodge", stat="identity")

figure2 <- figure2 + scale_fill_manual(name = 'Bill Type', values = c('gray5', 'gray50'), labels = c('Not Commemorative', 'Commemorative')) + labs(x = 'Congress', y = 'Number of Suspension Measures') + scale_x_continuous(breaks = seq(93,113,by = 1)) + theme_bw()

figure2 <- figure2 + theme(legend.position = 'bottom')

figure2


#### Figure 3 ####

load('maj_pct_susp.rdata')

figure3 <- ggplot(maj_pct_susp, aes(x = Cong, y = maj_pct)) +
  geom_line() + scale_x_continuous(breaks = seq(93,113,by = 1)) + scale_y_continuous(breaks = seq(0,100, by = 10), limits = c(0, 100)) + labs(x = 'Congress', y = 'Majority Party Share of Suspension Measures') + geom_point(aes(x = Cong, y = maj_pct)) + theme_bw()

figure3


#### Figure 4 ####

load('cmte_reported_susp_bills.rdata')

figure4 <- ggplot(cmte_reported_susp_bills, aes(fill=ReportH, y=n, x=Cong)) + 
  geom_bar(position="dodge", stat="identity")

figure4 <- figure4 + scale_fill_manual(name = 'Committee Action Status', values = c('gray5', 'gray50'), labels = c('Not Reported', 'Reported')) + labs(x = 'Congress', y = 'Number of Suspension Measures') + scale_x_continuous(breaks = seq(93,113,by = 1)) + theme_bw()

figure4 <- figure4 + theme(legend.position = 'bottom')

figure4

#### Models ####


load('models_replication_data_lsq.rdata')

minority_model_paper_version <- glmer.nb(all_susp_bills ~ (1|congress) + (1|icpsr) + previous_voteshare_scaled + seniority_scaled + all_bills_scaled + pct_restrictive_scaled + margin_scaled + tot_nonsusp_passed_scaled + total_bills_considered_scaled, data = subset(models_replication_data_lsq, models_replication_data_lsq$special_election == 0 & models_replication_data_lsq$party_switch == 0 & models_replication_data_lsq$left_office == 0 & models_replication_data_lsq$majority == 0), glmerControl(optim = 'bobyqa'))

summary(minority_model_paper_version)

majority_model_paper_version <- glmer.nb(all_susp_bills ~ (1|congress) + (1|icpsr) + previous_voteshare_scaled + seniority_scaled + all_bills_scaled + chair + subchr + moderate_majority + thirty_zone_member + pct_restrictive_scaled + margin_scaled + tot_nonsusp_passed_scaled + total_bills_considered_scaled, data = subset(models_replication_data_lsq, models_replication_data_lsq$special_election == 0 & models_replication_data_lsq$party_switch == 0 & models_replication_data_lsq$left_office == 0 & models_replication_data_lsq$majority == 1), glmerControl(optim = 'bobyqa'))

summary(majority_model_paper_version)

pooled_model_paper_version <- glmer.nb(all_susp_bills ~ (1|congress) + (1|icpsr) + previous_voteshare_scaled + seniority_scaled + all_bills_scaled + chair + subchr + majority + moderate_majority + thirty_zone_member + pct_restrictive_scaled + margin_scaled + tot_nonsusp_passed_scaled + total_bills_considered_scaled, data = subset(models_replication_data_lsq, models_replication_data_lsq$special_election == 0 & models_replication_data_lsq$party_switch == 0 & models_replication_data_lsq$left_office == 0), glmerControl(optim = 'bobyqa'))

summary(pooled_model_paper_version)

full_model_paper_version <- glmer.nb(all_susp_bills ~ (1|congress) + (1|icpsr) + previous_voteshare_scaled + seniority_scaled + all_bills_scaled + chair + subchr + moderate_majority + thirty_zone_member + majority + previous_voteshare_scaled * majority + pct_restrictive_scaled + margin_scaled + tot_nonsusp_passed_scaled + total_bills_considered_scaled, data = subset(models_replication_data_lsq, models_replication_data_lsq$special_election == 0 & models_replication_data_lsq$party_switch == 0 & models_replication_data_lsq$left_office == 0), glmerControl(optim = 'bobyqa'))

summary(full_model_paper_version)







#### Figure 6 ####

full_model_plot_version <- glmer.nb(all_susp_bills ~ (1|congress) + (1|icpsr) + previous_voteshare_scaled + seniority_scaled + all_bills_scaled + chair + subchr + moderate_majority + thirty_zone_member + bw_thirty_and_party_median + majority_status + previous_voteshare_scaled * majority_status + pct_restrictive_scaled + margin_scaled + tot_nonsusp_passed_scaled + total_bills_considered_scaled + ch_sp_rept_noncomm_cong_scaled, data = subset(models_replication_data_lsq, models_replication_data_lsq$special_election == 0 & models_replication_data_lsq$party_switch == 0 & models_replication_data_lsq$left_office == 0), glmerControl(optim = 'bobyqa'))

summary(full_model_plot_version)


figure6 <- plot_model(full_model_plot_version, type = 'int', axis.title = c('Scaled Previous Vote Share', 'Predicted Count of Suspension Measures'), colors = 'bw', title = '', axis.lim(.3,1), legend.title = 'Majority Status') + theme_bw()

figure6 <- figure6 + theme(legend.position = 'bottom')

figure6


#### Figure 7 ####

load('member_type_pcts.rdata')

figure7 <- ggplot(member_type_pcts_lsq, aes(fill=member_type, y=pct, x=Cong)) + 
  geom_bar( stat="identity") + theme_bw()

figure7 <- figure7 + scale_fill_manual(name = 'Member Type', values = c('gray5', 'gray20', 'gray50', 'gray80'), labels = c('Cmte. Chairs', 'Subcmte. Chairs', 'Maj. Party Rank and File', 'Min. Party Members')) + labs(x = 'Congress', y = 'Percentage of Suspension Measures') + scale_y_continuous(breaks = seq(0,100,by = 10)) + scale_x_continuous(breaks = seq(93,113,by = 1)) + theme(legend.position = 'bottom')

figure7


#### Figure 8 ####

load('figure8_data.rdata')

figure8 <- ggplot(data = figure8_data, aes(x = congress.x, y = avgs)) + 
  geom_line(aes(linetype = member_type_ordered)) + scale_x_continuous(breaks = seq(93,113,by = 1)) + scale_y_continuous(breaks = seq(0,10, by = 1), limits = c(0, 10)) + labs(x = 'Congress', y = 'Average Number of Suspension Measures', linetype = 'Member Type') + geom_point(aes(x = congress.x, y = avgs)) + theme_bw()

figure8 <- figure8 + theme(legend.position = 'bottom')

figure8


#### Figure 9, plot and models ####

pooled_comm_plot_model <- glmer.nb(comm_susp_bills ~ (1|congress) + (1|icpsr) + previous_voteshare_scaled + seniority_scaled + chair + subchr + moderate_majority + thirty_zone_member + majority + c_bills_scaled + pct_restrictive_scaled + margin_scaled + tot_nonsusp_passed_scaled + total_bills_considered_scaled, data = subset(models_replication_data_lsq, models_replication_data_lsq$special_election == 0 & models_replication_data_lsq$party_switch == 0 & models_replication_data_lsq$left_office == 0), glmerControl(optim = 'bobyqa'))


pooled_noncomm_plot_model <- glmer.nb(noncomm_susp_bills ~ (1|congress) + (1|icpsr) + previous_voteshare_scaled + seniority_scaled + chair + subchr + moderate_majority + thirty_zone_member + majority + noncomm_bills_scaled + pct_restrictive_scaled + margin_scaled + tot_nonsusp_passed_scaled + total_bills_considered_scaled, data = subset(models_replication_data_lsq, models_replication_data_lsq$special_election == 0 & models_replication_data_lsq$party_switch == 0 & models_replication_data_lsq$left_office == 0), glmerControl(optim = 'bobyqa'))

plot_models(pooled_comm_plot_model, pooled_noncomm_plot_model)









